package com.enation.app.javashop.core.trade.order.model.vo;

import com.enation.app.javashop.core.trade.order.model.enums.*;
import com.enation.app.javashop.core.trade.order.support.OrderOperateChecker;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

import java.io.Serializable;

/**
 * 订单可进行的操作
 * @author Snow create in 2018/5/15
 * @version v2.0
 * @since v7.0.0
 */
@JsonNaming(value = PropertyNamingStrategy.SnakeCaseStrategy.class)
public class OrderOperateAllowable implements Serializable {

    @ApiModelProperty(value = "是否允许被取消" )
    private Boolean allowCancel;

    @ApiModelProperty(value = "是否允许被确认" )
    private Boolean allowConfirm;

    @ApiModelProperty(value = "是否允许被支付" )
    private Boolean allowPay;

    @ApiModelProperty(value = "是否允许被发货" )
    private Boolean allowShip;

    @ApiModelProperty(value = "是否允许被收货" )
    private Boolean allowRog;

    @ApiModelProperty(value = "是否允许被评论" )
    private Boolean allowComment;

    @ApiModelProperty(value = "是否允许被完成" )
    private Boolean allowComplete;

    @ApiModelProperty(value = "是否允许申请售后" )
    private Boolean allowApplyService;

    @ApiModelProperty(value = "是否允许取消(售后)" )
    private Boolean allowServiceCancel;


    public Boolean getAllowCancel() {
        return allowCancel;
    }

    public void setAllowCancel(Boolean allowCancel) {
        this.allowCancel = allowCancel;
    }

    public Boolean getAllowConfirm() {
        return allowConfirm;
    }

    public void setAllowConfirm(Boolean allowConfirm) {
        this.allowConfirm = allowConfirm;
    }

    public Boolean getAllowPay() {
        return allowPay;
    }

    public void setAllowPay(Boolean allowPay) {
        this.allowPay = allowPay;
    }

    public Boolean getAllowShip() {
        return allowShip;
    }

    public void setAllowShip(Boolean allowShip) {
        this.allowShip = allowShip;
    }

    public Boolean getAllowRog() {
        return allowRog;
    }

    public void setAllowRog(Boolean allowRog) {
        this.allowRog = allowRog;
    }

    public Boolean getAllowComment() {
        return allowComment;
    }

    public void setAllowComment(Boolean allowComment) {
        this.allowComment = allowComment;
    }

    public Boolean getAllowComplete() {
        return allowComplete;
    }

    public void setAllowComplete(Boolean allowComplete) {
        this.allowComplete = allowComplete;
    }

    public Boolean getAllowApplyService() {
        return allowApplyService;
    }

    public void setAllowApplyService(Boolean allowApplyService) {
        this.allowApplyService = allowApplyService;
    }

    public Boolean getAllowServiceCancel() {
        return allowServiceCancel;
    }

    public void setAllowServiceCancel(Boolean allowServiceCancel) {
        this.allowServiceCancel = allowServiceCancel;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o){
            return true;
        }

        if (o == null || getClass() != o.getClass()){
            return false;
        }

        OrderOperateAllowable allowable = (OrderOperateAllowable) o;

        return new EqualsBuilder()
                .append(allowCancel, allowable.allowCancel)
                .append(allowConfirm, allowable.allowConfirm)
                .append(allowPay, allowable.allowPay)
                .append(allowShip, allowable.allowShip)
                .append(allowRog, allowable.allowRog)
                .append(allowComment, allowable.allowComment)
                .append(allowComplete, allowable.allowComplete)
                .append(allowApplyService, allowable.allowApplyService)
                .append(allowServiceCancel, allowable.allowServiceCancel)
                .isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37)
                .append(allowCancel)
                .append(allowConfirm)
                .append(allowPay)
                .append(allowShip)
                .append(allowRog)
                .append(allowComment)
                .append(allowComplete)
                .append(allowApplyService)
                .append(allowServiceCancel)
                .toHashCode();
    }

    @Override
    public String toString() {
        return "OrderOperateAllowable{" +
                "allowCancel=" + allowCancel +
                ", allowConfirm=" + allowConfirm +
                ", allowPay=" + allowPay +
                ", allowShip=" + allowShip +
                ", allowRog=" + allowRog +
                ", allowComment=" + allowComment +
                ", allowComplete=" + allowComplete +
                ", allowApplyService=" + allowApplyService +
                ", allowServiceCancel=" + allowServiceCancel +
                '}';
    }

    /**
     * 空构造器
     */
    public OrderOperateAllowable() {

    }


    /**
     * 根据各种状态构建对象
     * @param paymentTypeEnum
     * @param orderStatus
     * @param commentStatus
     * @param shipStatus
     * @param serviceStatus
     * @param payStatus
     */
    public OrderOperateAllowable(PaymentTypeEnum paymentTypeEnum, OrderStatusEnum orderStatus, CommentStatusEnum commentStatus,
                                 ShipStatusEnum shipStatus, ServiceStatusEnum serviceStatus, PayStatusEnum payStatus) {

        //是否允许被取消
        this.allowCancel = OrderOperateChecker.checkAllowable(paymentTypeEnum, orderStatus, OrderOperateEnum.CANCEL);
        //是否允许被确认
        this.allowConfirm = OrderOperateChecker.checkAllowable(paymentTypeEnum, orderStatus, OrderOperateEnum.CONFIRM);
        //是否允许被支付
        this.allowPay = OrderOperateChecker.checkAllowable(paymentTypeEnum, orderStatus, OrderOperateEnum.PAY);
        //是否允许被发货
        this.allowShip = OrderOperateChecker.checkAllowable(paymentTypeEnum, orderStatus, OrderOperateEnum.SHIP)
                && !ServiceStatusEnum.APPLY.name().equals(serviceStatus.name())
                && !ServiceStatusEnum.PASS.name().equals(serviceStatus.name());
        //是否允许被收货
        this.allowRog = OrderOperateChecker.checkAllowable(paymentTypeEnum, orderStatus, OrderOperateEnum.ROG);
        //是否允许被评论
        this.allowComment = CommentStatusEnum.UNFINISHED.value().equals(commentStatus.value())&& ShipStatusEnum.SHIP_ROG.value().equals(shipStatus.value());
        //是否允许被完成
        this.allowComplete = OrderOperateChecker.checkAllowable(paymentTypeEnum, orderStatus, OrderOperateEnum.COMPLETE);

        boolean defaultServiceStatus = ServiceStatusEnum.NOT_APPLY.value().equals(serviceStatus.value());

        //货到付款
        if(PaymentTypeEnum.COD.name().equals(paymentTypeEnum.name())){

            //是否允许被申请售后 = 已收货 && 未申请过售后 && 订单是已收货状态
            allowApplyService = ShipStatusEnum.SHIP_ROG.value().equals(shipStatus.value())
                    && defaultServiceStatus
                    && ShipStatusEnum.SHIP_ROG.value().equals(shipStatus.value());

        }else{
            //是否允许被申请售后 = 已付款 && 未申请过售后 && 订单是已收货状态
            allowApplyService = PayStatusEnum.PAY_YES.value().equals(payStatus.value())
                    && defaultServiceStatus
                    && ShipStatusEnum.SHIP_ROG.value().equals(shipStatus.value());

            //订单是否允许取消(售后) = 支付状态已付款  &&  订单已付款状态
            this.allowServiceCancel = PayStatusEnum.PAY_YES.value().equals(payStatus.value())
                    && OrderStatusEnum.PAID_OFF.value().equals(orderStatus.value())
                    && (ServiceStatusEnum.NOT_APPLY.name().equals(serviceStatus.name())
                        ||ServiceStatusEnum.EXPIRED.name().equals(serviceStatus.name()));

        }

    }


}
